
 import { put, takeEvery } from 'redux-saga/effects'
 import apiType, {
    CREATE_STORE, EDIT_STORE, GET_STORE_INFO,
    GET_STORE_LIST,
    GET_STORE_SELECTION,
} from "@constants/ActionTypes";
 import {getAxios, postAxios, putAxios} from "@utils/api";

 /**
  * 获取门店下拉框
  * @param status
  * @param search
  * @param callback
  * @returns {{callback: *, type: string, params: {search, status}}}
  */
 export const getStoreSelection = ({ status, search } = {}, callback) => ({
     type: apiType(GET_STORE_SELECTION).request,
     params: {
         status,
         search
     },
     callback
 })

 /**
  * 获取门店列表
  * @param busScope
  * @param status
  * @param search
  * @param callback
  * @returns {{callback: *, type: string, params: {search: *, busScope: *, status: *}}}
  */
 export const getStoreList = ({ busScope, status, search }, callback) => ({
     type: apiType(GET_STORE_LIST).request,
     params: {
         busScope,
         status,
         search
     },
     callback
 })

 /**
  * 获取店铺详情
  * @param storeId
  * @param callback
  * @returns {{callback: *, type: string, params: {storeId: *}}}
  */
 export const getStoreInfo = ({ storeId }, callback) => ({
     type: apiType(GET_STORE_INFO).request,
     params: {
         storeId
     },
     callback
 })

 /**
  * 创建门店
  * @param params
  * @param callback
  * @returns {{callback: *, type: string, params: *}}
  */
 export const createStore = (params, callback) => ({
     type: apiType(CREATE_STORE).request,
     params,
     callback
 })

 /**
  * 编辑门店
  * @param params
  * @param callback
  * @returns {{callback: *, type: string, params: *}}
  */
 export const editStore = (params, callback) => ({
     type: apiType(EDIT_STORE).request,
     params,
     callback
 })
 
 const sagas = {
     * [apiType(GET_STORE_SELECTION).request]({params, callback}) {
         try {
             const response = yield getAxios('/admin/api/brand/v1/stores', params)
             yield put({
                 type: apiType(GET_STORE_SELECTION).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(GET_STORE_SELECTION).failure});
         }
     },
     * [apiType(GET_STORE_LIST).request]({params, callback}) {
         try {
             const response = yield getAxios('/admin/api/brand/v1/stores', params)
             yield put({
                 type: apiType(GET_STORE_LIST).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(GET_STORE_LIST).failure});
         }
     },
     * [apiType(GET_STORE_INFO).request]({params, callback}) {
         try {
             const response = yield getAxios(`/admin/api/brand/v1/stores/${params.storeId}`)
             yield put({
                 type: apiType(GET_STORE_INFO).success,
                 response,
                 params
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(GET_STORE_LIST).failure});
         }
     },
     * [apiType(CREATE_STORE).request]({params, callback}) {
         try {
             const response = yield postAxios('/admin/api/brand/v1/store/create', params)
             yield put({
                 type: apiType(CREATE_STORE).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(CREATE_STORE).failure});
         }
     },
     * [apiType(EDIT_STORE).request]({params, callback}) {
         try {
             const response = yield putAxios('/admin/api/brand/v1/store/update', params)
             yield put({
                 type: apiType(EDIT_STORE).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(EDIT_STORE).failure});
         }
     },
 }
 
 
 export default Object.keys(sagas).map(item => {
     return function * s() {
         yield takeEvery(item, function *(args) {
             try {
                 yield sagas[item](args)
             } catch (e) {
                 console.log(e)
             }
         })
     }
 })